﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using RilattackLibrary;
using RilattackGameWPF;
using System.Threading;
using System.ServiceModel;
using System.Timers;

namespace RilattackGameWPF
{
    /// <summary>
    /// Logique d'interaction pour App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static RilattackGame Game;
        public static MainWindow Window;
        public static CallBack Client { get; set; }
        public static IServer Server { get; set; }

        public static void ConnectToLobby()
        {
            EndpointAddress endpoint = new EndpointAddress(Window.serveurEndpoint.Text);
            Client = new CallBack();
            DuplexChannelFactory<IServer> factory = new DuplexChannelFactory<IServer>(Client, new NetTcpBinding(SecurityMode.None), endpoint);
            try
            {
                Server = factory.CreateChannel();
                if (Server != null)
                {
                    if (!Server.login(Window.NickName.Text))
                    {
                        MessageBox.Show("Le pseudo est deja utilisé !");
                    }
                    else
                    {
                        Client.CurrentPlayer = new ControlablePlayer();
                        Client.CurrentPlayer.Nickname = Window.NickName.Text;
                        Client.CurrentPlayer.UID = Server.getMyUID();
                        Window.connexionBtn.IsEnabled = false;
                    }
                }
            }
            catch
            {
                MessageBox.Show("Impossible de se connecter au serveur !");
            }
        }
        public static void LaunchGame()
        {
            Window.LaunchGame();
        }


    }
    
}
